package cn.datawin.bms.modules.deploy.controller;

import cn.datawin.bms.deploy.entity.DeplBranchInfoEntity;
import cn.datawin.bms.modules.deploy.service.DeplBranchInfoService;
import cn.datawin.common.core.util.R;
import cn.datawin.common.log.annotation.SysLog;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发布分支表
 *
 * @author datawin
 * @date 2025-03-13 11:10:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/deploy/branchInfo" )
@Tag(description = "branchInfo" , name = "发布分支表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DeplBranchInfoController {

    private final DeplBranchInfoService deplBranchInfoService;

    /**
     * 分页查询
     * @param branchInfo 发布分支表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('deploy_branchInfo_view')" )
    public R getBranchInfoPage(@RequestParam(defaultValue = "1") long current,
							   @RequestParam(defaultValue = "20") long size, @ParameterObject DeplBranchInfoEntity branchInfo) {
		Page<DeplBranchInfoEntity> page = new Page<>(current, size);
		LambdaQueryWrapper<DeplBranchInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(DeplBranchInfoEntity::getType,branchInfo.getType());
		wrapper.eq(DeplBranchInfoEntity::getStatus,branchInfo.getStatus());
        return R.ok(deplBranchInfoService.page(page, wrapper));
    }

	/**
	 * 查询所有稳定且启用的分支
	 */
	@Operation(summary = "查询所有分支" , description = "查询所有分支" )
	@GetMapping("/listAll" )
	@PreAuthorize("isAuthenticated()")
	public R getBranchInfoList() {
		LambdaQueryWrapper<DeplBranchInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(DeplBranchInfoEntity::getStatus,0);
		wrapper.eq(DeplBranchInfoEntity::getType,1);
		return R.ok(deplBranchInfoService.list(wrapper));
	}


    /**
     * 通过id查询发布分支表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('deploy_branchInfo_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(deplBranchInfoService.getById(id));
    }

    /**
     * 新增发布分支表
     * @param branchInfo 发布分支表
     * @return R
     */
    @Operation(summary = "新增发布分支表" , description = "新增发布分支表" )
    @SysLog("新增发布分支表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('deploy_branchInfo_add')" )
    public R save(@RequestBody DeplBranchInfoEntity branchInfo) {
        return R.ok(deplBranchInfoService.save(branchInfo));
    }

    /**
     * 修改发布分支表
     * @param branchInfo 发布分支表
     * @return R
     */
    @Operation(summary = "修改发布分支表" , description = "修改发布分支表" )
    @SysLog("修改发布分支表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('deploy_branchInfo_edit')" )
    public R updateById(@RequestBody DeplBranchInfoEntity branchInfo) {
        return R.ok(deplBranchInfoService.updateById(branchInfo));
    }

    /**
     * 通过id删除发布分支表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除发布分支表" , description = "通过id删除发布分支表" )
    @SysLog("通过id删除发布分支表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('deploy_branchInfo_del')" )
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(deplBranchInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param branchInfo 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('deploy_branchInfo_export')" )
    public List<DeplBranchInfoEntity> export(DeplBranchInfoEntity branchInfo, Integer[] ids) {
        return deplBranchInfoService.list(Wrappers.lambdaQuery(branchInfo).in(ArrayUtil.isNotEmpty(ids), DeplBranchInfoEntity::getId, ids));
    }
}
